<template>
	<view class="page">
		<u-navbar title="我的跑步" border @leftClick="$utils.back()" placeholder />
		<view class="tabBox viewWidth" v-if="!loading">
			<view v-for="item in typeList" :key="item" @click="type=item" :class="{'choose':type==item}">{{item}}</view>
		</view>
		<view class="tabBox viewWidth" v-if="type!='跑步'&&!loading">
			<view style="border: none;display: flex;align-items: center;" @click="dateShow=true">
				{{date?date:'请选择时间'}}
				<view @click.stop="()=>{}">
					<u-icon @click="clear" name='close-circle' size='28rpx' v-if="date" />
				</view>
			</view>
			<view style="flex: 1;border: none;" />
			<u-icon :label='`：${users}个`' name="account" size="40rpx"></u-icon>
			<view style="width: 5rpx;border: none;" />
			<u-icon :label='`：￥${totalAmount}`' name="red-packet" size="40rpx"></u-icon>
		</view>
		<view class="body">
			<z-paging ref="paging" style="height: 100%;" :fixed="false" v-model="data" @query="query"
				:default-page-no="1" :default-page-size="10" @scroll='scroll'>
				<view class="viewWidth">
					<active v-if="type=='跑步'" :list='data' :loading='loading' ref='active' :playIndex='playIndex'
						@reload='$refs.paging.reload()' @ListloadingChange='loading!=loading' />
					<template v-else>
						<template v-if="!loading">
							<view v-for="item in data" :key="item.id" class="userItem">
								<image class="image" :src="item.sender.avatar" mode="aspectFill"></image>
								<view class="flex">
									<view style="display: flex;justify-content: space-between;">
										<view style="max-width: 60%;">
											<u-text :text='item.sender.nick_name' :lines='1' size='30rpx' />
										</view>


									</view>
									<view style="display: flex;justify-content: space-between;">
										<view style="display: flex;">
											<u-icon name='red-packet-fill' color='red'
												:label='`${item.red_packet>0?"+":""}${item.red_packet}`'
												:labelColor="item.red_packet>0?'green':'red'" />
											<view style="width: 10rpx;" />
											<u-text size='24rpx' :lines='1' :text='item.title' color='#ddd' />
										</view>
										<view>
											{{item.created_at | changeTime}}
										</view>
									</view>
								</view>
							</view>
						</template>
						<template v-else>
							<view v-for="item in 10" :key="item" class="userItem">
								<view class="image Skeleton"></view>
								<view class="flex">
									<view style="height: 40rpx;" class="Skeleton"></view>
									<view style="height: 30rpx;" class="Skeleton"></view>
								</view>
							</view>
						</template>
					</template>
				</view>
			</z-paging>
		</view>
		<view class="add" @click="jumpAdd">
			<u-icon color='#fff' name='plus' size='50rpx' />
		</view>
		<view>
			<u-datetime-picker title='选择时间' :maxDate='maxDate' @cancel='dateShow=false;' @close='dateShow=false'
				:show="dateShow" v-model="date2" mode="year-month" @confirm='confirm'></u-datetime-picker>
		</view>
	</view>
</template>

<script>
	import active from './components/active'
	export default {

		components: {
			active,
		},
		data() {
			return {
				date2: null,
				maxDate: Number(new Date()),
				dateShow: false,
				date: null,
				users: null,
				totalAmount: null,
				typeList: ['跑步', '红包'],
				type: '跑步',
				scrollTop: 0,
				playIndex: 0,
				data: [],
				loading: true,
				user: uni.getStorageSync('user'),
				openid: uni.getStorageSync('openid')
			}
		},
		onLoad() {},
		watch: {
			type() {
				this.loading = true
				this.$refs.paging.reload()
			}
		},
		methods: {
			clear() {
				this.date = null
				this.date2 = null
				this.$refs.paging.reload()
			},
			confirm(e) {
				var today = new Date(e.value)

				var tomorrowYear = today.getFullYear();
				var tomorrowMonth = today.getMonth() + 1; // 月份从0开始，所以要加1
				this.date = tomorrowYear + "-" + tomorrowMonth
				this.dateShow = false
				this.$refs.paging.reload()
			},
			jumpAdd() {
				this.$utils.navigateTo({
					url: '/run/add'
				})
			},
			query(page, size) {
				let obj = {
					page,
					size,

				}
				uni.showLoading({
					title: '加载中...'
				})
				if (this.type == '跑步') {
					this.$api.My.Run(obj).then(res => {
						uni.hideLoading()
						this.$refs.paging.complete(res.data.data)
						this.loading = false
					}).catch(err => {
						uni.hideLoading()
						uni.$u.toast(err.data.message)
					})
				} else {
					if (this.date) {
						obj.date = this.date
					}
					this.$api.My.Redpacket(obj).then(res => {
						uni.hideLoading()
						this.$refs.paging.complete(res.data.blessings.data)
						this.totalAmount = res.data.totalAmount
						this.users = res.data.blessings.total
						this.loading = false
					}).catch(err => {
						uni.hideLoading()
						uni.$u.toast(err.data.message)
					})
				}

			},
			scroll(e) {
				const top = e.detail.scrollTop
				this.scrollTop = top * 2
				let index = 0
				let domTop0 = 0
				const query = uni.createSelectorQuery().in(this.$refs.active);
				query.selectAll('.activeBox').boundingClientRect(data => {
					data.forEach((rect, domIndex) => {
						let domTop = rect.top + top
						if (domIndex == 0) {
							domTop0 = domTop
						}
						if (top > (domTop - domTop0)) {
							index = domIndex
						}
						if (domIndex == data.length - 1) {
							this.playIndex = index

						}
					});
				}).exec();
			},
		}
	}
</script>

<style lang="scss" scoped>
	::v-deep .uni-select {
		border: none !important;
	}

	.page {
		padding: 0rpx 20rpx;
		height: 100vh;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 10rpx);
		/* 兼容 iOS 设备 */
		padding-bottom: calc(env(safe-area-inset-bottom) + 10rpx);
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
	}

	.body {
		margin-top: 10rpx;
		flex: 1;
		overflow: hidden;
	}

	.tabBox {
		display: flex;
		margin: 20rpx 0;

		>view {
			border-radius: 8rpx;
			border: 1rpx solid #ddd;
			padding: 5rpx 10rpx;
			margin-right: 20rpx;
		}
	}

	.userItem {
		display: flex;
		margin: 20rpx 0 0 0;
		position: relative;

		.image {
			width: 100rpx;
			height: 100rpx;
			border-radius: 8rpx;
			margin-right: 20rpx;
		}

		.flex {
			height: 100rpx;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			padding-bottom: 10rpx;
			box-sizing: border-box;
			border-bottom: 1rpx solid #eee;
			flex: 1;

		}
	}

	.choose {
		color: #fff !important;
		background-color: rgb(66, 83, 216) !important;
		border: none;
	}

	.add {
		position: fixed;
		right: 5vw;
		bottom: 20vh;
		border-radius: 100%;
		box-shadow: 0 0 10rpx #ddd;
		background: rgba(0, 0, 0, 0.3);
		padding: 20rpx;
		z-index: 2;

	}
</style>